﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using System.Windows.Forms;

namespace UIShare
{
    public static class FileExport
    {
        public static void ExportToExcel(GridControl grid, string fileName)
        {
            string filePath = showSaveFileDialog(fileName, "Excel 97-2003 工作簿(*.xls)|*.xls");

            if (string.IsNullOrEmpty(filePath))
                return;


            UIOperManager.SaveData(
                () =>
                {
                    grid.ExportToXls(filePath);
                    return true;
                },
                "导出成功.");

        }

        private static string showSaveFileDialog(string fileName, string filter)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = fileName;
            saveFileDialog.Filter = filter;
            DialogResult result = saveFileDialog.ShowDialog();
            if(result == DialogResult.OK)
                return saveFileDialog.FileName;

            return string.Empty;
        }
    }
}
